package com.gzhu.knowledgeAdmin.controller;


import com.gzhu.knowledgeAdmin.common.dto.param.ParamAddRequest;
import com.gzhu.knowledgeAdmin.common.dto.param.ParamDeleteRequest;
import com.gzhu.knowledgeAdmin.common.dto.param.ParamGetRequest;
import com.gzhu.knowledgeAdmin.common.dto.param.ParamSimpleListRequest;
import com.gzhu.knowledgeAdmin.common.exception.BusinessException;
import com.gzhu.knowledgeAdmin.common.result.ErrorCode;
import com.gzhu.knowledgeAdmin.common.result.R;
import com.gzhu.knowledgeAdmin.parammanage.ParamCRUD;
import com.gzhu.knowledgeAdmin.parammanage.ParamCRUDRegistry;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/template")
public class ParamController {

    @Autowired
    private ParamCRUDRegistry paramCRUDRegistry;

//    @GetMapping("/param/all")
//    public BaseResponse<Map<String, List<String>>> getAllParam() {
//        Map<String, ParamCRUD> typeDataSourceMap = paramCRUDRegistry.getTypeDataSourceMap();
//        HashMap<String, List<String>> resultMap = new HashMap<>();
//
//        for (String type : typeDataSourceMap.keySet()) {
//            ParamCRUD paramCRUD = typeDataSourceMap.get(type);
//            List singleList = paramCRUD.getSingleList();
//            resultMap.put(type,singleList);
//        }
//
//        return R.success(resultMap);
//    }

    @PostMapping("/param/simpleList")
    public R getSimpleList(@RequestBody ParamSimpleListRequest paramSimpleListRequest) {

        String type = paramSimpleListRequest.getType();

        if (StringUtils.isNullOrEmpty(type)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        ParamCRUD paramCRUD = paramCRUDRegistry.getParamCURDByType(type);

        List<String> list = paramCRUD.getSimpleList();

        return R.ok(list);
    }

    @PostMapping("/param")
    public R getList(@RequestBody ParamGetRequest paramGetRequest) {

        String type = paramGetRequest.getType();
        if (StringUtils.isNullOrEmpty(type)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        ParamCRUD paramCRUD = paramCRUDRegistry.getParamCURDByType(type);

        Page list = paramCRUD.getList(paramGetRequest);

        return R.ok(list);
    }

    @PostMapping("/param/add")
    public R addParam(@RequestBody ParamAddRequest paramAddRequest) {

        String type = paramAddRequest.getType();
        if (StringUtils.isNullOrEmpty(type)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String value = paramAddRequest.getValue();
        if (StringUtils.isNullOrEmpty(value)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        ParamCRUD paramCRUD = paramCRUDRegistry.getParamCURDByType(type);

        paramCRUD.addParam(paramAddRequest);

        return R.ok(true);
    }

    @PostMapping("/param/delete")
    public R deleteParam(@RequestBody ParamDeleteRequest paramDeleteRequest) {

        String type = paramDeleteRequest.getType();
        if (StringUtils.isNullOrEmpty(type)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        if (paramDeleteRequest.getIds().isEmpty()) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"删除值不能为空");
        }

        ParamCRUD paramCRUD = paramCRUDRegistry.getParamCURDByType(type);

        paramCRUD.deleteParam(paramDeleteRequest);

        return R.ok(true);
    }
}
